package cn.bulibuqi.myblog.bean;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.Data;
import org.springframework.core.metrics.StartupStep;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author bulibuqi
 * @create 2021-12-13:55
 */
@Data
@TableName("blog")//默认是类名
public class Blog {

    @TableId(type = IdType.AUTO)
    private Long id;
    private String title;
    private String content;
    private String firstPicture;
    private String flag;
    private Long views;
    private Boolean appreciation;
    private Boolean  shareStatement;
    private Boolean commentabled;
    private Boolean published;
    private Boolean recommend;
    private Date createTime;
    private Date updateTime;
    private Long userId;
    private Long typeId;
    @TableField(exist = false)

    private String tagIds;
    @TableField(exist = false)
    private List<Tag> tags = new ArrayList<>();


    public void setTitle(String title) {
        if ("".equals(title)){
            this.title=null;
        }
        else{
            this.title = title;
        }
    }

    public List<Long> stringToList(String s){
        String[] split = s.split(",");
        List<Long> list = new ArrayList<Long>();
        for (String s1 : split) {
            list.add(Long.parseLong(s1));
        }

        return list;

    }



    // 初始化tagIds格式为 1，2，3这样
    public void init(){
        this.tagIds = tagsToIds(tags);
    }

    private String tagsToIds(List<Tag> tags){
        if (!tags.isEmpty()){
            StringBuffer ids = new StringBuffer();
            boolean flag = false;
            for (Tag tag : tags) {
                if (flag){
                    ids.append(",");
                }else{
                    flag = true;
                }
                ids.append(tag.getId());
            }
            return ids.toString();
        }else {
            return tagIds;
        }

    }



    @TableField(exist = false)
    private List<Comment> comments = new ArrayList<>();
    @TableField(exist = false)
    private Type type;

}
